home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / smail-3.1.28 / pd / getopt / get_opt.c next >
Encoding:
C/C++ Source or Header  |  1991-12-08  |  2.4 KB  |  95 lines

  1. /*
  2.  * @(#)pd/getopt/get_opt.c    1.4 12/9/91 01:57:22
  3.  */
  4.  
  5. /*
  6.  * Posted to the net by: Mike Patnode  (mpatnode@polyslo.UUCP)
  7.  *
  8.  * Here's something you've all been waiting for:  the AT&T public domain
  9.  * source for getopt(3).  It is the code which was given out at the 1985
  10.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  11.  * directly from AT&T.  The people there assure me that it is indeed
  12.  * in the public domain.
  13.  * 
  14.  * There is no manual page.  That is because the one they gave out at
  15.  * UNIFORUM was slightly different from the current System V Release 2
  16.  * manual page.  The difference apparently involved a note about the
  17.  * famous rules 5 and 6, recommending using white space between an option
  18.  * and its first argument, and not grouping options that have arguments.
  19.  * Getopt itself is currently lenient about both of these things White
  20.  * space is allowed, but not mandatory, and the last option in a group can
  21.  * have an argument.  That particular version of the man page evidently
  22.  * has no official existence, and my source at AT&T did not send a copy.
  23.  * The current SVR2 man page reflects the actual behavor of this getopt.
  24.  * However, I am not about to post a copy of anything licensed by AT&T.
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include "defs.h"
  29.  
  30. #ifndef HAVE_GETOPT
  31.  
  32. /*LINTLIBRARY*/
  33. #define ERR(s, c)    if(opterr){\
  34.     extern int write();\
  35.     char errbuf[2];\
  36.     errbuf[0] = c; errbuf[1] = '\n';\
  37.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  38.     (void) write(2, s, (unsigned)strlen(s));\
  39.     (void) write(2, errbuf, 2);}
  40.  
  41. extern char *index();
  42.  
  43. int    opterr = 1;
  44. int    optind = 1;
  45. int    optopt;
  46. char    *optarg;
  47.  
  48. int
  49. getopt(argc, argv, opts)
  50. int    argc;
  51. char    **argv, *opts;
  52. {
  53.     static int sp = 1;
  54.     register int c;
  55.     register char *cp;
  56.  
  57.     if(sp == 1)
  58.         if(optind >= argc ||
  59.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  60.             return(EOF);
  61.         else if(strcmp(argv[optind], "--") == NULL) {
  62.             optind++;
  63.             return(EOF);
  64.         }
  65.     optopt = c = argv[optind][sp];
  66.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  67.         ERR(": illegal option -- ", c);
  68.         if(argv[optind][++sp] == '\0') {
  69.             optind++;
  70.             sp = 1;
  71.         }
  72.         return('?');
  73.     }
  74.     if(*++cp == ':') {
  75.         if(argv[optind][sp+1] != '\0')
  76.             optarg = &argv[optind++][sp+1];
  77.         else if(++optind >= argc) {
  78.             ERR(": option requires an argument -- ", c);
  79.             sp = 1;
  80.             return('?');
  81.         } else
  82.             optarg = argv[optind++];
  83.         sp = 1;
  84.     } else {
  85.         if(argv[optind][++sp] == '\0') {
  86.             sp = 1;
  87.             optind++;
  88.         }
  89.         optarg = NULL;
  90.     }
  91.     return(c);
  92. }
  93.  
  94. #endif /* not HAVE_GETOPT */
  95.